<?php
namespace Client\Table;

use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;

use Zend\Paginator\Paginator;
use Zend\Db\Sql\Where;
use Zend\Db\ResultSet\ResultSet;

class ProductTable extends CommonTable
{
    protected $table = 'hy_product';
    protected $id = 'product_id';
    
    public function fetchAll($cate_id, $limit, $paginated = false) {
     

    }
    public function getOrgProductsRecommended($orgIds){
    	$select = new Select($this->getTable());
    	$select->columns( array(
    		'org_id','product_id','product_name','product_price'
    	));
    	$select->where(array('org_id' => $orgIds));
    	$rowset_ranking = $this->selectWith($select);
    	$row = $rowset_ranking->toArray();
    	return $row;	 
    }
    
    public function getAllRecommendedProducts(){
    	if(empty($orgId)){
    		throw new \Exception("The Field orgId is invalid!");
    	}
    	$select = new Select($this->getTable());
    	$select->columns( array(
    			'org_id','product_id','product_name','product_price'
    	));
    	$select->where(array('recommend'=>1));
    	$rowset_ranking = $this->selectWith($select);
    	$row = $rowset_ranking->toArray();
    	
    	return $row;
    	
    }

    /**
     * 根据单位类目ID取商品
     * @param unknown $cate_id
     * @return unknown
     */
    public function getProListByCateId($cate_id)
    {
        $select = new Select($this->getTable());
        $select->columns( array(
            "*"
        ));
    
        $select->join(
            array('pc' => 'hy_product_category'),
            'pc.product_id = hy_product.product_id',
            array() ,
            $select::JOIN_RIGHT
            );
    
        $select->where(array('pc.category_id' => $cate_id,'hy_product.product_is_display'=>1));
        $select->order(array('top desc'));
    
        $rowset_ranking = $this->selectWith($select);
        $row = $rowset_ranking->toArray();
    
        return $row;
    }

    public function findByOrgId($org_ids, $limit = 5)
    {
        foreach ($org_ids as $key => $org) {
                $select = new Select(array('p' => $this->getTable()));
                $select->columns(array('product_id', 'product_name'));
                $select->join(
                    array('o' => 'hy_organization'),
                    'o.org_id=p.org_id',
                    array(),
                    $select::JOIN_LEFT
                    );
                $select->where(array('p.recommend' => 1));
                $select->where(array('p.org_id' => $org['org_id']));
                $select->limit($limit);
                $rowset = $this->selectWith($select);
                $result = $rowset->toArray();
                
                $results[$key]['org_thumb_image'] =  $org['org_thumb_image'];
                $results[$key]['org_name'] =  $org['org_name'];
                $results[$key]['org_id'] =  $org['org_id'];
                $results[$key]['child'] =  $result;
        }
     
        return $results;
        
    }
    /**
     * 取得购买商品信息
     * @param unknown $cate_id
     * @return unknown
     */
    public function getBuyProList($ProList)
    {
        $select = new Select($this->getTable());
        $select->columns( array(
            'product_id','product_name','product_price','product_thumb_image'
        ));
        $select->where(array('product_id' => $ProList));
        $rowset_ranking = $this->selectWith($select);
        $row = $rowset_ranking->toArray();
    
        return $row;
    }
    

}